package com.pengtu.gsj.dao.system;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.pengtu.gsj.dao.hibernate.HibernateDao;
import com.pengtu.gsj.entity.app.Code;

/**
 * 
 * @auto wy
 * @date 2017年4月1日
 * @describe
 */
@Repository
public class CodeDao extends HibernateDao<Code, String>{

	/**
	 * 
	 * getCodeByCategory:
	 * 适用: 根据category 与 codeId 查询对应的Code
	 * @param category
	 * @param codeId
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public Code getCodeByCategory(Integer category, String codeId) {
		Criterion categoryC = Restrictions.eq("category", category);
		Criterion codeIdC = Restrictions.eq("codeId", codeId);
		return findUnique(categoryC,codeIdC);
	}

	public String getCodeNameByCategory(Integer category, String codeId) {
		if(StringUtils.isEmpty(codeId)){
			return ""; 
		}
		Criterion categoryC = Restrictions.eq("category", category);
		Criterion codeIdC = Restrictions.eq("codeId", codeId);
		 return findUnique(categoryC,codeIdC).getCodeName();
	}

	public Code getCodeIdByCategory(Integer category, String codeName) {
		Criterion categoryC = Restrictions.eq("category", category);
		Criterion codeNameC = Restrictions.eq("codeName", codeName);
		Code c =  findUnique(categoryC,codeNameC);
		if(c == null){
			throw new RuntimeException("根据category与名称codeName未查询到码表记录");
		}else{
		 return c;
		}
	}

	public Code getCodeLikeByCategoryCodeName(Integer category, String codeName) {
		Criterion categoryC = Restrictions.eq("category", category);
		Criterion codeNameC = Restrictions.like("codeName", codeName,MatchMode.ANYWHERE);
		Code c =  findUnique(categoryC,codeNameC);
		if(c == null){
			throw new RuntimeException("根据category与名称codeName未查询到码表记录,原因：category:"+category+"codeName:"+codeName);
		}else{
			return c;
		}
	}

	/**
	 * deleteAll:
	 * 适用: 
	 * @exception 
	 * @since  1.0.0
	*/
	public void deleteAll() {
		String hql = "delete Code c";
		this.getSession().createQuery(hql).executeUpdate();
	}
	
}
